package com.service.business.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.business.member.controller.rep.FeedbackDetailRep;
import com.service.business.member.controller.rep.FeedbackPageRep;
import com.service.business.member.controller.req.FeedbackPageReq;
import com.service.business.member.controller.req.FeedbackReq;
import com.service.business.member.service.FeedbackService;
import com.service.business.notify.service.NotifyMessageService;
import com.service.business.support.SessionUtil;
import com.service.core.exception.CommonException;
import com.service.core.rest.response.CommonPageRep;
import com.service.dao.enums.FeedbackEnum;
import com.service.dao.enums.NotifyMessageEnums;
import com.service.dao.mapper.FeedbackMapper;
import com.service.dao.mapper.NotifyMessageMapper;
import com.service.dao.model.Feedback;
import com.service.dao.model.NotifyMessage;
import com.service.utility.modelmapper.ModelMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service("feedbackService")
@SuppressWarnings("ALL")
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private NotifyMessageMapper notifyMessageMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(FeedbackReq feedbackReq) {
        Feedback feedback = ModelMapperUtil.getStrictModelMapper().map(feedbackReq, Feedback.class);
        feedback.setMemberAccount(sessionUtil.getUser().getAccount());
        feedback.setMemberName(sessionUtil.getUser().getUsername());
        feedback.setCreateTime(new Date());
        feedback.setFeedbackStatus(FeedbackEnum.Status.created.getCode());
        save(feedback);
    }

    @Override
    public CommonPageRep<FeedbackPageRep> pageFeedback(FeedbackPageReq pageReq) {
        Page<Feedback> page = PageHelper.startPage(pageReq.getPageNo(), pageReq.getPageSize());
        baseMapper.page4MemberByMemberId(pageReq.getMemberId());
        List<FeedbackPageRep> pageList = page.getResult().stream().map(item -> ModelMapperUtil
                .getStrictModelMapper().map(item, FeedbackPageRep.class)).collect(Collectors.toList());
        return CommonPageRep.buildResultRep(page, pageList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FeedbackDetailRep detail(Integer id) {
        Feedback feedback = getById(id);

        if(ObjectUtils.isEmpty(feedback)){
            throw new CommonException("该反馈不存在", "feedback_not_exist");
        }

        if (ObjectUtils.nullSafeEquals(feedback.getFeedbackStatus(), FeedbackEnum.Status.reply.getCode())) {
            updateById(Feedback.builder()
                    .id(feedback.getId())
                    .feedbackStatus(FeedbackEnum.Status.checked.getCode())
                    .build());
        }

        NotifyMessage notifyMessage = new NotifyMessage();
        notifyMessage.setMessageType(NotifyMessageEnums.FEED_BACK_TYPE.getCode());
        notifyMessage.setMemberId(feedback.getMemberId());
        notifyMessage.setTargetId(id);
        notifyMessage.setCreateTime(new Date());
        notifyMessage.setIsRead(NotifyMessageEnums.IS_READ.getCode());
        notifyMessageMapper.insert(notifyMessage);

        return ModelMapperUtil.map(feedback, FeedbackDetailRep.class);
    }

    @Override
    public List<Feedback> listNotReadReplyByMemberId(Integer memberId) {
        return list(new LambdaQueryWrapper<Feedback>()
                .eq(Feedback::getMemberId, memberId)
                .eq(Feedback::getFeedbackStatus, FeedbackEnum.Status.reply.getCode()));
    }
}
